<template>
  <div class="login-page">
    <!-- 导航栏 -->
    <van-nav-bar title="面经登录" />

    <!-- 表单 -->
    <van-form @submit="onSubmit">
      <!-- 输入框 -->
      <van-field
        v-model="user.username"
        label="用户名"
        placeholder="请输入用户名"
        :rules="rules.username"
      />
      <van-field
        v-model="user.password"
        type="password"
        label="密码"
        placeholder="密码"
        :rules="rules.password"
      />
      <div style="margin: 16px;">
        <van-button square block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <router-link class="link" to="/register">注册账号</router-link>
  </div>
</template>

<script>
import { AxiosLogin } from '@/api/user'
import { setToken } from '@/utils/token'
export default {
  name: 'login-page',
  data () {
    return {
      user: {
        username: '',
        password: ''
      },
      rules: {
        user: [
          {
            required: true,
            message: '请填写用户名'
          },
          { patternn: /^\w{5,}$/, message: '账号至少5位' }
        ],
        password: [
          {
            required: true,
            message: '请填写密码'
          },
          { patternn: /^\w{6,}$/, message: '密码至少6位' }
        ]
      }
    }
  },
  methods: {
    async onSubmit () {
      const { data: res } = await AxiosLogin(this.user)
      this.$toast.success('登录成功')
      setToken(res.token)
      this.$router.push('/')
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
